﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Reflection;
using DotNetNuke.Entities.Content;
using System.Collections;

namespace Dynamo
{
    public class AdminPermissions
    {
        public static string LoadScript(int TabId, int ModuleId)
        {
            DotNetNuke.Entities.Modules.ModuleController controller = new DotNetNuke.Entities.Modules.ModuleController();
            Hashtable settings = controller.GetModuleSettings(ModuleId);
            if (settings != null && settings.Contains("ContentTypeID"))
            {
                string value = (settings["ContentTypeID"] as string);
                int ctid = 0;
                if (int.TryParse(value, out ctid))
                {
                    ContentController cc = new ContentController();
                    ContentItem item = cc.GetContentItem(ctid);
                    if (item != null && !string.IsNullOrEmpty(item.Content))
                    {
                        return item.Content;
                    }
                }
            }

            return null;
        }
        public static void SaveScript(string Script, int TabId, int ModuleId)
        {
            ContentController cc = new ContentController();
            string term = string.Format("Dynamo_{0}_{1}", TabId, ModuleId);
            IQueryable<ContentItem> items = cc.GetContentItemsByTerm(term);
            ContentTypeController ctc = new ContentTypeController();
            var lst = (from c in ctc.GetContentTypes()
                       where c.ContentType == "Dynamo_Script"
                       select c.ContentTypeId);

            int ct = -1;
            if (lst.Count() == 0)
            {
                ct = ctc.AddContentType(new ContentType("Dynamo_Script"));
            }
            else
            {
                ct = lst.First();
            }

            int id = 0;
            bool insert = true;
            ContentItem ci = null;
            if (items != null)
            {
                ci = (items.Take(1) as ContentItem);
                if (ci != null)
                {
                    insert = false;
                    id = ci.ContentItemId;
                }
            }
            if (insert)
            {
                ci = new ContentItem();
                ci.Content = Script;
                ci.ContentTypeId = ct;
                ci.TabID = TabId;
                ci.ModuleID = ModuleId;
                id = cc.AddContentItem(ci);
            }
            else
            {
                ci.Content = Script;
                cc.UpdateContentItem(ci);
            }

            DotNetNuke.Entities.Modules.ModuleController controller = new DotNetNuke.Entities.Modules.ModuleController();
            controller.UpdateModuleSetting(ModuleId, "ContentTypeID", id.ToString());

 
        }

        public static bool IgnoreSecurity
        {
            get
            {
                string val = DotNetNuke.Entities.Portals.PortalController.GetPortalSetting("Scripting.IgnoreSecurity", DotNetNuke.Entities.Portals.PortalSettings.Current.PortalId, "false");
                if (val != null && val.Trim() != "" && val.Trim().ToLower() == "true") return true;
                return false;
            }
            set
            {
                DotNetNuke.Entities.Portals.PortalController.UpdatePortalSetting(DotNetNuke.Entities.Portals.PortalSettings.Current.PortalId, "Scripting.IgnoreSecurity", value.ToString(), true);
            }
        }
        public static bool AllowCLR
        {
            get
            {
                string val = DotNetNuke.Entities.Portals.PortalController.GetPortalSetting("Scripting.AllowCLR", DotNetNuke.Entities.Portals.PortalSettings.Current.PortalId, "false");
                if (val != null && val.Trim() != "" && val.Trim().ToLower() == "true") return true;
                return false;
            }
            set
            {
                DotNetNuke.Entities.Portals.PortalController.UpdatePortalSetting(DotNetNuke.Entities.Portals.PortalSettings.Current.PortalId, "Scripting.AllowCLR", value.ToString(), true);
            }
        }

        public static bool ScriptingEnabled
        {
            get
            {
                string val = DotNetNuke.Entities.Portals.PortalController.GetPortalSetting("Scripting.Enabled", DotNetNuke.Entities.Portals.PortalSettings.Current.PortalId, "false");
                if (val != null && val.Trim() != "" && val.Trim().ToLower() == "true") return true;
                return false;
            }
            set
            {
                DotNetNuke.Entities.Portals.PortalController.UpdatePortalSetting(DotNetNuke.Entities.Portals.PortalSettings.Current.PortalId, "Scripting.Enabled", value.ToString(), true);
            }
        }
        public static string EnabledPermissions
        {
            get
            {
                return DotNetNuke.Entities.Portals.PortalController.GetPortalSetting("Scripting.Permissions", DotNetNuke.Entities.Portals.PortalSettings.Current.PortalId, "");
            }
            set
            {
                DotNetNuke.Entities.Portals.PortalController.UpdatePortalSetting(DotNetNuke.Entities.Portals.PortalSettings.Current.PortalId, "Scripting.Permissions", value.ToString(), true);
            }
        }


        private static List<System.Type> iPermissionTypeList = null;
        private static object _PLock = new object();
        public static List<System.Type> IPermissionTypeList
        {
            get
            {
                if (iPermissionTypeList != null) return iPermissionTypeList;
                lock (_PLock)
                {
                    List<System.Type> l = GetIPermissionTypes();
                    iPermissionTypeList = l;
                }
                return iPermissionTypeList;
            }
        }
        private static List<System.Type> GetIPermissionTypes()
        {
            var TypeList = new System.Collections.Generic.List<System.Type>();
            try
            {
                var binFolder = new System.IO.DirectoryInfo(System.Web.HttpContext.Current.Server.MapPath("bin"));
                var list = binFolder.GetFiles("*.dll");
                var asmList = from fileInfo in list select Assembly.LoadFile(fileInfo.FullName);
                TypeList.Add(typeof(System.Security.Permissions.FileIOPermission));
                TypeList.Add(typeof(System.Security.Permissions.ReflectionPermission));

                foreach (Assembly a in asmList.ToList())
                {
                    try
                    {
                        foreach (Type t in a.GetTypes())
                        {
                            if (t.IsPublic && t.IsSubclassOf(typeof(System.Security.IPermission)))
                                TypeList.Add(t);

                        }
                    }
                    catch (Exception)
                    {
                    }
                }
            }
            catch (Exception)
            {
            }
            return TypeList;
        }
    }
}